﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class MyAdmin_NhaVanChuyen_AM : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //Phân quyền
        if (Helper.getCurrentAdminQuyen(this.Master) == QuyenTruyCap.QuyenXem)
        {
            // xử lý quyền xem
            ((CommandField)gvDSNhaVanChuyen.Columns[0]).ShowDeleteButton = false;
            CommandField dtCmdField = ((CommandField)dtvNhaVanChuyen_AM.Fields[dtvNhaVanChuyen_AM.Fields.Count - 1]);
            dtCmdField.ShowEditButton = false;
            dtCmdField.ShowInsertButton = false;
            XoaNhieuBUTTON.Visible = false;
        }
        else if (Helper.getCurrentAdminQuyen(this.Master) == QuyenTruyCap.QuyenChinhSua)
        {
            // xử lý quyền chỉnh sửa
        }

        lblThongBaoDTView.Visible = false;
        lblThongBaoGridView.Visible = false;
    }
    private void refreshDetailViewPageIndex()
    {
        dtvNhaVanChuyen_AM.DataBind();
        dtvNhaVanChuyen_AM.PageIndex = Helper.calPageIndexForDetailView(gvDSNhaVanChuyen);
    }
    protected void XoaNhieuBUTTON_Click(object sender, EventArgs e)
    {
        string ids = "";
        List<long> deletedIDs = new List<long>();
        int lastCellIndex = gvDSNhaVanChuyen.Rows[0].Cells.Count - 1;
        foreach (GridViewRow r in gvDSNhaVanChuyen.Rows)
        {
            CheckBox checkXoa = (CheckBox)r.Cells[lastCellIndex].FindControl("checkXoa");
            if (checkXoa.Checked)
            {
                long id = long.Parse(r.Cells[1].Text);
                deletedIDs.Add(id);
                ids += id.ToString() + ", ";
            }
        }

        lblThongBaoGridView.Visible = true;
        lblThongBaoGridView.Text = "Xóa thành công record: " + ids.Substring(0, ids.LastIndexOf(", "));

        try
        {
            DataAccess_Layer.NhaVanChuyen_DAC.delete(deletedIDs);
        }
        catch (System.Exception ex)
        {
            lblThongBaoGridView.Text = "Xóa thất bại!";
        }
        
        gvDSNhaVanChuyen.DataBind();
        dtvNhaVanChuyen_AM.DataBind();
    }
    protected void gvDSNhaVanChuyen_SelectedIndexChanged(object sender, EventArgs e)
    {
        refreshDetailViewPageIndex();
    }
    protected void dtvNhaVanChuyen_AM_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
    {
        gvDSNhaVanChuyen.DataBind();
        Helper.changeGVPageIndex(gvDSNhaVanChuyen, gvDSNhaVanChuyen.PageCount - 1);
        gvDSNhaVanChuyen.SelectedIndex = gvDSNhaVanChuyen.Rows.Count - 1;
        refreshDetailViewPageIndex();
    }
    protected void dtvNhaVanChuyen_AM_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
    {
        lblThongBaoDTView.Visible = true;
        string id = e.Keys[0].ToString();

        if (e.Exception == null)
        {
            lblThongBaoDTView.Text = "Cập nhật thành công record có ID là " + id;
            return;
        }

        lblThongBaoDTView.Text = "Cập nhật thất bại record!";
        e.ExceptionHandled = true;

        gvDSNhaVanChuyen.DataBind();
        refreshDetailViewPageIndex();  
    }
    protected void gvDSNhaVanChuyen_RowDeleted(object sender, GridViewDeletedEventArgs e)
    {
        if (e.Exception != null)
        {
            string undeletedID = e.Keys[0].ToString();
            Response.Write(Helper.CreateMessageBox("Không thể xóa Record có ID " + undeletedID));
            e.ExceptionHandled = true;
        }
        else
        {
            long id = long.Parse(e.Keys[0].ToString());
            Response.Write(Helper.CreateMessageBox("Đã xóa Record có ID = " + id));
            dtvNhaVanChuyen_AM.DataBind();
        }
    }
    protected void gvDSNhaVanChuyen_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        Helper.changeGVPageIndex(gvDSNhaVanChuyen, e.NewPageIndex);
        gvDSNhaVanChuyen.SelectedIndex = gvDSNhaVanChuyen.Rows.Count > 0 ? 0 : -1;
        gvDSNhaVanChuyen.PageIndex = Helper.calPageIndexForDetailView(gvDSNhaVanChuyen);
    }
    protected void gvDSNhaVanChuyen_PageIndexChanged(object sender, EventArgs e)
    {
        refreshDetailViewPageIndex();
    }
    protected void LQDSHoaDon_AM_Inserted(object sender, LinqDataSourceStatusEventArgs e)
    {
        lblThongBaoDTView.Visible = true;
        DataAccess_Layer.NhaVanChuyen newOrder = (DataAccess_Layer.NhaVanChuyen)e.Result;

        if (e.Exception == null)
        {
            lblThongBaoDTView.Text = "Thêm thành công record có ID là " + newOrder.ID;
            return;
        }

        lblThongBaoDTView.Text = "Thêm thất bại record!";
        e.ExceptionHandled = true;
    }
}